package com.example.service.impl;

import com.example.mapper.basicAuditInstructionMapp;
import com.example.pojo.basicAuditInstructionPojo;
import com.example.service.basicAuditInstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ClassName:basicAuditInstructionImpl
 * Package:com.example.service.impl
 * Description:
 *
 * @Author：牧星苑
 * @Create 2025/10/2 14:53
 * @Version 1.0
 */
@Service
public class basicAuditInstructionImpl implements basicAuditInstructionService {
    @Autowired
    private basicAuditInstructionMapp basicAuditInstructionMapp;
    @Value("${file.upload-dir}")
    private String saveDir;
    //新增一条审核块
    public void insertOne(basicAuditInstructionPojo pojo){
        pojo.setCreate_time(LocalDateTime.now());
        pojo.setUpdate_time(LocalDateTime.now());
        basicAuditInstructionMapp.insertOne(pojo);
    }

    //编辑一条审核块
    public void editOneById(basicAuditInstructionPojo pojo){
        pojo.setUpdate_time(LocalDateTime.now());
        basicAuditInstructionMapp.editOneById(pojo);
    }

    //分页查询审核块
    public List<basicAuditInstructionPojo> queryByPage(Map<String, Object> map){
        Integer page =(Integer) map.get("page");
        Integer size=(Integer) map.get("size");
        Integer startRow=page*size;
        List<basicAuditInstructionPojo> basicAuditInstructionPojos=basicAuditInstructionMapp.queryByPage(startRow, size);
        return basicAuditInstructionPojos;
    }

    //查询所有审核块条数
    public Integer queryQuantity(){
        Integer quantity= basicAuditInstructionMapp.queryQuantity();
        return quantity;
    }

    //根据一个字段修改一个字段
    public void editOneField(String id, String fieldName, String fieldValue){
        basicAuditInstructionMapp.editOneField(id, fieldName, fieldValue);
    }

    //下载审核块作业指导书的附件（图片、视频）
    public byte[] readFile(String fileName){
        File file = new File(saveDir + fileName);
        // 检查文件是否存在且可读
        if (file.exists() && file.canRead()) {
            try (FileInputStream fis = new FileInputStream(file)) {
                // 读取文件内容到byte[]数组中
                byte[] fileContent = fis.readAllBytes();
                return fileContent; // 返回文件内容的byte[]数组
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 如果文件不存在或不可读，或者读取时发生异常，返回null
        System.out.println("文件不存在或不可读：" + file.getAbsolutePath());
        return null;
    }
}
